<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use AlibabaCloud\SDK\Live\V20161101\Live;
use AlibabaCloud\Credentials\Credential;
use AlibabaCloud\Credentials\Credential\Config;
use AlibabaCloud\SDK\Ecs\V20140526\Ecs;
use AlibabaCloud\SDK\Ecs\V20140526\Models\DescribeInstancesRequest;
use AlibabaCloud\SDK\Ecs\V20140526\Models\DescribeSecurityGroupAttributeRequest;
use AlibabaCloud\SDK\Ecs\V20140526\Models\DescribeSecurityGroupsRequest;
use AlibabaCloud\SDK\Live\V20161101\Models\DescribeLiveStreamHistoryUserNumRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Illuminate\Support\Facades\Log;

class AliCloudManagerController extends Controller
{
    //
    public function test(){
        $credConfig = new Config([
            'type' => 'access_key',
            'accessKeyId' => 'LTAI5tBjZKEF9eHnxQxrbua2',
            'accessKeySecret' => 'VSItcPasGutI49BEywRwjrK06uSVTB',
            "endpoint"=>"ecs.cn-chengdu.aliyuncs.com"
        ]);

        $credClient = new Credential($credConfig);
        $esc=new Ecs($credConfig);

          $describeSecuretyGroupattrRequest=new DescribeSecurityGroupAttributeRequest([
            "securityGroupId"=>"sg-2vchfzk87skhbkjm255p",
            "regionId"=>"cn-chengdu"
          ]);//参数是蛇形命名 起始小后面以次大
            // $describeInstancesRequest = new DescribeInstancesRequest([
            //     "pageSize" => 100,
            //     "regionId" => 'cn-chengdu'
            // ]);
            // $resp = $esc->describeInstances($describeInstancesRequest);
            $resp=$esc->describeSecurityGroupAttribute($describeSecuretyGroupattrRequest);
            $instances = $resp->body;
            var_dump(json_encode($instances));
            // ob_start();
            // // foreach($instances as $instance){
            // //    echo("  " . $instance->hostName . " 实例ID " . $instance->instanceId . " CPU:" . (string) ($instance->cpu) . "  内存:" . (string) ($instance->memory) . " MB 规格：" . $instance->instanceType . " 系统:" . $instance->OSType . "(" . $instance->OSName . ") 状态：" . $instance->status . "");
            // // }
            // // ob_end_flush();
        // $credential = $credClient->getCredential();
        // $credential->getAccessKeyId();
        // $credential->getAccessKeySecret();

    }
    public static function getLiveRoomState($domain,$appName,$streamName,$startTime,$endTime){
        $credConfig = new \Darabonba\OpenApi\Models\Config([
            'type' => 'access_key',
            'accessKeyId' => 'LTAI5tBjZKEF9eHnxQxrbua2',
            'accessKeySecret' => 'VSItcPasGutI49BEywRwjrK06uSVTB',
            "endpoint"=>"live.aliyuncs.com"
        ]);
        $live=new Live($credConfig);
        $req=new DescribeLiveStreamHistoryUserNumRequest([
          "domainName"=>$domain,
          "appName"=>$appName,
          "streamName"=>$streamName,
          "startTime"=>$startTime,
          "endTime"=>$endTime,
        ]);
        $res=$live->describeLiveStreamHistoryUserNumWithOptions($req, new RuntimeOptions([]));
        return $res;
    }
}
